package com.gm.controller;

import com.gm.entity.User;
import com.gm.service.UserService;
import com.gm.util.FilesUtils;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/role")
public class roleController {
    @Autowired
    UserService userService;

    /**
     * 人员列表的查询
     * @param request
     * @return
     */
    @RequestMapping("/toRoleList")
    public String toRoleList(HttpServletRequest request){
        List<User> userList=userService.getUserList();
        request.setAttribute("userList",userList);
        return "admin/Role/RoleList";
    }
    /**
     * 去往添加页面
     */
    @RequestMapping("/addUser")
    public String addUser(){
        return "admin/Role/RoleInsert";
    }
    /**
     *人员的添加
     */

    @RequestMapping("/RoleInsert")
    public String RoleInsert(MultipartFile file,HttpServletRequest request,User user){
        String filesUpload_xm = FilesUtils.FilesUpload_xm(request, file, "\\upload");
        user.setHead(filesUpload_xm);
        System.out.println(user.toString());
        String s = UUID.randomUUID().toString();
        user.setId(s);
        userService.InsertUser(user);
        request.setAttribute("redirectUrl","role/toRoleList");
        return "admin/closeLayer";
    }
    /**
     * 修改页面
     */
    @RequestMapping("/toupdateUser")
    public String toupdateUser(String UsersId,HttpServletRequest request){
        System.out.println(UsersId);
        User userId=userService.getByIdUser(UsersId);
        request.setAttribute("user",userId);
        return "admin/Role/RoleUpdate";
    }
    /**
     * 修改
     */
    @RequestMapping("/Roleupdate")
    public String Roleupdate(MultipartFile file,HttpServletRequest request,User user){
        String filesUpload_xm = FilesUtils.FilesUpload_xm(request, file, "\\upload");
        user.setHead(filesUpload_xm);
        userService.roleupdate(user);
        request.setAttribute("redirectUrl","role/toRoleList");
        return "admin/closeLayer";
    }
    /**
     * 删除用户
     */
    @ResponseBody
    @RequestMapping("deleteUser")
    public String deleteUser(String UsersId){
        String i = userService.deleteUser(UsersId);
        if(i!=null){
            i="1";
        }
        return i;
    }
    /**
     * 删除用户跳转(不会js直接关闭刷新，除此下策)
     */
    @RequestMapping("todelete")
    public String todelete(HttpServletRequest request){
        return "redirect:/role/toRoleList";
    }
    /**
     * 停用
     */
    @RequestMapping("stop")
    @ResponseBody
    public boolean stop(HttpServletRequest request,String userId){
        System.out.println(userId);
        boolean flag=false;
        User user=new User();
        user.setId(userId);
        user.setDisable("2");
        int i=userService.stopUser(user);
        if (i>0) {
            flag=true;
        }
        return flag;
    }
}
